package screen.component
{
	import com.adobe.serialization.json.JSON;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.text.TextField;
	import helper.Services;
	import helper.ValueObject;
	import screen.SacombankScreen;
	/**
	 * ...
	 * @author ...
	 */
	public class GetReward extends UI
	{
		private var _get_reward : MovieClip;
		private static var instance : GetReward;
		private var _service:Services;
		private var _reward_loader:URLLoader;
		
		public function GetReward() 
		{
			if(!instance){
				instance = this;
			}			
			init();			
		}
		public static function getInstance(): GetReward{
			if(!instance){
				instance = new GetReward();
			}
			return instance;
		}
		public function init():void {
			_service = Services.getInstance();
			_reward_loader = new URLLoader();
			showRewardPopup();
		}
		private function showRewardPopup():void {
			_reward_loader.addEventListener(Event.COMPLETE,getRewardComplete);
			_reward_loader.addEventListener(IOErrorEvent.IO_ERROR,getRewardError);
			_reward_loader.load(_service.getRewardRequest());
		}
		private function getRewardComplete(e:Event):void {
			_reward_loader.removeEventListener(Event.COMPLETE,getRewardComplete);
			_reward_loader.removeEventListener(IOErrorEvent.IO_ERROR, getRewardError);
			var ob : Object = JSON.decode(e.target.data);
			if (ob.profit > 0 || ob.reward > 0) {
				_get_reward = ValueObject.S2_RESOURCE.getExportedAsset("getReward") as MovieClip;
				popupInit(_get_reward);
				popupShow(_get_reward);
				_get_reward.x = ValueObject.WIDTH / 2;
				_get_reward.y = ValueObject.HEIGHT / 2 + 120;
				buttonImageInit(_get_reward.okBtn);
				MovieClip(_get_reward.okBtn).addEventListener(MouseEvent.CLICK, hideRewardPopup);
				var result_txt:String = '<font color="#851300" size="14">Chào mừng bé quay lại game Phù Đổng!<font/>';
				result_txt  += '<br/><font color="#851300" size="13"> Bé vừa nhận được một số xu vào tài khoản, bao gồm:<font/>';
				result_txt  += '<br/><font color="#786617" size="13"> - Quà tặng quay lại chơi game: ' + ob.reward + ' xu<font/>';
				result_txt  += '<br/><font color="#786617" size="13"> - Lãi suất gửi ngân hàng: ' + ob.profit + ' xu<font/>';
				TextField(_get_reward.contentTf).htmlText = result_txt;
			}
		}
		private function getRewardError(e:IOErrorEvent):void{
			_reward_loader.removeEventListener(Event.COMPLETE,getRewardComplete);
			_reward_loader.removeEventListener(IOErrorEvent.IO_ERROR, getRewardError);
		}
		private function hideRewardPopup(e:MouseEvent):void {
			popupHide(_get_reward);
		}
	}

}